Android极速开发一(Apk安装)

安装之前判断是否有root权限,如果有root权限就静默安转,如果没有就利用意图进行安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
*
* @author Javen
* @since 2016/05/24
*/
public class ApkController {
/**
* 描述: 安装
*/
public static boolean install(String apkPath,Context context){
// 先判断手机是否有root权限
if(hasRootPerssion()){
// 有root权限,利用静默安装实现
return clientInstall(apkPath);
}else{
// 没有root权限,利用意图进行安装
File file = new File(apkPath);
if(!file.exists())
return false;
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
context.startActivity(intent);
return true;
}
}
/**
* 描述: 卸载
*/
public static boolean uninstall(String packageName,Context context){
if(hasRootPerssion()){
// 有root权限,利用静默卸载实现
return clientUninstall(packageName);
}else{
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI);
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uninstallIntent);
return true;
}
}
/**
* 判断手机是否有root权限
*/
private static boolean hasRootPerssion(){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 静默安装
*/
private static boolean clientInstall(String apkPath){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("chmod 777 "+apkPath);
PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
PrintWriter.println("pm install -r "+apkPath);
// PrintWriter.println("exit");
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 静默卸载
*/
private static boolean clientUninstall(String packageName){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
PrintWriter.println("pm uninstall "+packageName);
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 启动app
* com.exmaple.client/.MainActivity
* com.exmaple.client/com.exmaple.client.MainActivity
*/
public static boolean startApp(String packageName,String activityName){
boolean isSuccess = false;
String cmd = "am start -n " + packageName + "/" + activityName + " \n";
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(process!=null){
process.destroy();
}
}
return isSuccess;
}
private static boolean returnResult(int value){
// 代表成功
if (value == 0) {
return true;
} else if (value == 1) { // 失败
return false;
} else { // 未知情况
return false;
}
}
/**
* 查询所有非系统app的信息
* @param mContext
* @return
*/
public static List<Map<String, Object>> getAPPInstalled(Context mContext) {
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
// 获取系统内的所有程序信息
Intent mainintent = new Intent(Intent.ACTION_MAIN, null);
mainintent.addCategory(Intent.CATEGORY_LAUNCHER);
List<PackageInfo> packageinfo = mContext.getPackageManager()
.getInstalledPackages(0);
int count = packageinfo.size();
for (int i = 0; i < count; i++) {
PackageInfo pinfo = packageinfo.get(i);
ApplicationInfo appInfo = pinfo.applicationInfo;
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
// 系统程序 忽略
} else {
// 非系统程序
Map<String, Object> map = new HashMap<String, Object>();
map.put("app_logo", pinfo.applicationInfo.loadIcon(mContext
.getPackageManager()));
map.put("app_name", pinfo.applicationInfo.loadLabel(mContext
.getPackageManager()));
map.put("package_name", pinfo.applicationInfo.packageName);
map.put("app_version_name", pinfo.versionName);
map.put("app_version_code", pinfo.versionCode);
listItems.add(map);
}
}
return listItems;
}
/**
* 判断应用是否需要安装
* @param mContext
* @param packageName
* @param versionCode
* @return
*/
public static boolean isInstalled(Context mContext,String packageName,int versionCode) {
// 获取系统内的所有程序信息
Intent mainintent = new Intent(Intent.ACTION_MAIN, null);
mainintent.addCategory(Intent.CATEGORY_LAUNCHER);
List<PackageInfo> packageinfo = mContext.getPackageManager()
.getInstalledPackages(0);
int count = packageinfo.size();
String pn;
int vc;
for (int i = 0; i < count; i++) {
PackageInfo pinfo = packageinfo.get(i);
ApplicationInfo appInfo = pinfo.applicationInfo;
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
// 系统程序 忽略
} else {
// 非系统程序
pn=pinfo.applicationInfo.packageName;
vc=pinfo.versionCode;
if (pn.equalsIgnoreCase(packageName) && vc >= versionCode) {
return true;
}
}
}
return false;
}
}
Javen wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!